home *** CD-ROM | disk | FTP | other *** search
- //=========================================================================================================
- //
- // Copyright 2002 Macromedia, Inc. All rights reserved.
- //
- // Feature: Paste Fix
- // Author: JDH
- // Module: PasteManager.js
- // Purpose: Main Paste Manager class (and entry points).
- // Updates:
- // 5/17/02 - Started file control
- // 5/31/02 - Added more comments
- //
- //=========================================================================================================
-
- // The paste manager which handles the entire clipboard conversion process
-
- function PasteManager()
- {
- // Initialize the phases array
-
- this.phases = new Array();
-
- // Put together the initial list of content handlers
-
- var handlers = new Array();
- handlers.push( new ParseMetaTags );
- handlers.push( new IdentifyMSApplications );
- handlers.push( new FixupMSGarbage );
- handlers.push( new RetainStructure );
- handlers.push( new DecomposeClasses );
- handlers.push( new RemoveUnsupportedAttributes );
- handlers.push( new RemoveParsingRequiredStructuralTags );
- handlers.push( new RemoveCSSClasses );
- handlers.push( new DemoteToParagraphs );
- handlers.push( new SingleSpaceParagraphs );
- handlers.push( new MergeRedundantFontTags );
- handlers.push( new ChangeToStrongAndEm );
- handlers.push( new ConvertEmptyDivs );
-
- // Iterate through the content handlers and put organize them by
- // priority number into the phases member.
-
- for( index in handlers )
- {
- // Get the phase descriptor from the handler
-
- var phase_desc = handlers[ index ].getPhase();
-
- // Get the phase priority
-
- var phase = phase_desc.priority;
-
- // If the phase is currently empty then create the array to put the
- // handlers into
-
- if ( this.phases[ phase ] == null )
- {
- this.phases[ phase ] = new Array();
- this.phases[ phase ].name = phase_desc.name;
- this.phases[ phase ].elements = new Array();
- }
-
- // Push the handler into the handlers list for this phase number
-
- this.phases[ phase ].elements.push( handlers[ index ] );
- }
- }
-
- PasteManager.prototype.run = PasteManager_run;
- PasteManager.prototype.getClipHTML = PasteManager_getClipHTML;
- PasteManager.prototype.getDebugText = PasteManager_getDebugText;
- PasteManager.prototype.getDebugHTML = PasteManager_getDebugHTML;
-
- function PasteManager_run( clipDOM, clipCSS, targetDOM, targetCSS, settings )
- {
- // Put together the new context
-
- this.pasteContext = new PasteContext( clipDOM, clipCSS, targetDOM, targetCSS, settings );
-
- // Start the debug trace
-
- for( var setting in settings )
- this.pasteContext.debugInformation( "PasteManager", "Setting : " + setting );
- this.pasteContext.debugInformation( "PasteManager", "Start run" );
-
- // Iterate through the phases
-
- for ( var phase_index = 0; phase_index < PHASE_MAX; phase_index++ )
- {
- // If we have elements in this phase then iterate through them
-
- if ( this.phases[ phase_index ] )
- {
- // Put out the debugging information
-
- this.pasteContext.debugInformation( "PasteManager", "Phase: " + this.phases[ phase_index ].name );
-
- // Get the phase elements and interate through them and run each one
-
- var phase = this.phases[ phase_index ].elements;
-
- for( handler_index in phase )
- {
- var handler = phase[ handler_index ];
- handler.run( this.pasteContext );
- }
- }
- }
-
- // Finish the debug trace
-
- if ( 0 ) alert( this.pasteContext.getClipText() );
-
- this.pasteContext.debugInformation( "PasteManager", "End run" );
-
- this.pasteContext.updateClipDOM();
-
- // Return the output size
-
- var findClippingScanner = new FindClippingScanner();
- html = findClippingScanner.scan( this.pasteContext.getClipText(), this.pasteContext );
- return html.length;
- }
-
- function PasteManager_getDebugText( ) { return this.pasteContext.getDebugText(); }
-
- function PasteManager_getDebugHTML( ) { return this.pasteContext.getDebugHTML(); }
-
- function PasteManager_getClipHTML( )
- {
- // JDH: This is pretty brute force. First we turn the whole document into text, then we
- // find the start and end of the fragment, suck it out, and then remove the markers from the
- // finished piece.
-
- var full_html = this.pasteContext.getClipText();
-
- full_html = full_html.replace( /[\r\n]/g, " " );
-
- var out_html = full_html.match( /\<body\>(.*?)\<\/body\>/g );
- out_html = out_html.toString();
-
- out_html = out_html.replace( /\<body\>/, "" );
- out_html = out_html.replace( /\<\/body\>/, "" );
- out_html = out_html.replace( /\<\!\-\-(\s*)StartFragment(\s*)\-\-\>/, "" );
- out_html = out_html.replace( /\<\!\-\-(\s*)EndFragment(\s*)\-\-\>/, "" );
-
- return out_html;
- }
-
-
-
- function smartPaste( bSilent, inputDOM, returnValue )
- {
- // Check to make sure we aren't trying to paste in something huge
-
- if ( bSilent == false )
- {
- var text = inputDOM.documentElement.outerHTML;
- if ( text.length > MM.od_MaxThreshold)
- {
- alert( MM.MSG_odStop );
- returnValue[ 0 ] = true;
- return;
- }
- if ( text.length > MM.od_WarnThreshold )
- {
- if ( ! confirm( MM.MSG_odWarn ) )
- {
- returnValue[ 0 ] = true;
- return;
- }
- }
- }
-
- try {
-
- // Put together a new paste manager
-
- var mgr = new PasteManager();
-
- // Define the settings for the paste
-
- var settings = {};
- settings[ SETTINGS_CONTRIBUTE ] = 1;
-
- // Look for Contribute specific settings
-
- if ( dreamweaver.appName == "Contribute" )
- {
- if ( dreamweaver.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates() &&
- !dreamweaver.getDocumentDOM().isTemplateInstance())
- settings[ SETTINGS_ETO ] = 1;
-
- if ( dreamweaver.getDocumentDOM().getCCSharedSetting_FontsEmitFontOrSpan() == "font" )
- settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] = 1;
-
- if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_FontsLetUserChange() )
- settings[ SETTINGS_NO_CSS ] = 1;
-
- if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleHTMLHeadings() )
- settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ] = 1;
-
- // commented this out for bug: 85013 -- think we're going to decide that
- // show CSS styles and insert CSS style should be separate. So we shouldn't disable
- // inserting Word text with <span> tags even if we don't allow users to set CSS styles.
-
- // if ( ! dreamweaver.getDocumentDOM().getCCSharedSetting_StyleCSS() )
- // settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] = 1;
-
- if ( dreamweaver.getDocumentDOM().getCCSharedSetting_SingleSpaceParagraphsCSS() )
- settings[ SETTINGS_SINGLE_SPACE_P ] = 1;
- }
-
- // We always use <STRONG> and <EM> instead of <B> and <I>, respectively.
- settings[ SETTINGS_USE_EMPHASIS ] = 1;
-
- // Debug code for settings
-
- if ( 0 ) {
- if( settings[ SETTINGS_ETO ] ) alert( "settings[ SETTINGS_ETO ]" );
- if( settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] ) alert( "settings[ SETTINGS_CHANGE_SPAN_TO_FONT ]" );
- if( settings[ SETTINGS_NO_CSS ] ) alert( "settings[ SETTINGS_NO_CSS ]" );
- if( settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ] ) alert( "settings[ SETTINGS_DEMOTE_TO_PARAGRAPHS ]" );
- if( settings[ SETTINGS_CHANGE_SPAN_TO_FONT ] ) alert( "settings[ SETTINGS_CHANGE_SPAN_TO_FONT ]" );
- if( settings[ SETTINGS_SINGLE_SPACE_P ] ) alert( "settings[ SETTINGS_SINGLE_SPACE_P ]" );
- }
-
- // Get the CSS definitions from the clipboard
-
- var clipCSS = new CSSClassCollection();
-
- Utils_LoadCSSFromDOM( inputDOM, clipCSS );
-
- // Get the names of the classes in the target
-
- var targetCSS = new CSSReferenceClassCollection();
-
- // Here we add referenced classes to the target CSS representative. All that we
- // use is the class name.
- var styles = dreamweaver.cssStylePalette.getStyles();
- for ( var index in styles )
- targetCSS.add( styles[ index ] );
-
- var outputSize = mgr.run( inputDOM, clipCSS, null, targetCSS, settings );
-
- if ( outputSize < 1 )
- throw( "No HTML was generated" );
-
- dw.forceGarbageCollection();
-
- if ( dreamweaver.appName == "Contribute" )
- formatSource( inputDOM );
-
- }
- catch( e )
- {
- alert( MM.MSG_odProblem );
- dw.getDocumentDOM().clipPasteText();
- returnValue[ 0 ] = true;
- }
-
- return true;
- }
-
-
- // =============================================================================================================
- // =============================================================================================================
- //
- // Test code
- //
- // =============================================================================================================
- // =============================================================================================================
-
- function testPaste( testName, clipFile, settingsStr, targetDOM, debug, addHeaders, inputDOM )
- {
- var totalTime = null;
-
- if ( addHeaders )
- targetDOM.setView( "design" );
-
- if ( clipFile.length > 0 )
- JDHUtils.copyFromFile( "c:/pf/clips/" + clipFile );
-
- // Read the clipboard into a string
-
- var str = JDHUtils.readHTMLClipboard();
-
- if ( str.length > 0 )
- {
- var prefixHTML = "";
- if ( addHeaders )
- {
- prefixHTML += "<h1> Test: " + testName + "</h1>";
- prefixHTML += "<font face=Courier>Clipboard File: " + clipFile + "</font><br>";
- prefixHTML += "<font face=Courier>Settings: " + settingsStr + "</font><br>";
- prefixHTML += "<hr>";
- }
-
- // Build an "offscreen" DOM to parse the document
-
- if ( inputDOM == null )
- {
- var fwURL = dw.getConfigurationPath() + "/Shared/MM/Cache/empty.htm";
- DWfile.write( fwURL, '' );
- var clipDOM = dw.getDocumentDOM( fwURL );
- clipDOM.documentElement.outerHTML = "";
- clipDOM.body.innerHTML = str;
- }
- else
- clipDOM = inputDOM;
-
- // Put together a new paste manager
-
- var mgr = new PasteManager();
-
- // Define the settings for the paste
-
- var settings = {};
- settings[ SETTINGS_CONTRIBUTE ] = 1;
-
- settingsValues = settingsStr.split( "," );
- for( setting_index in settingsValues )
- {
- var settingsValue = Utils_StripWhitespace( settingsValues[ setting_index ] );
- settings[ eval( settingsValue ) ] = 1;
- }
-
- // Get the CSS definitions from the clipboard
-
- var clipCSS = new CSSClassCollection();
- Utils_LoadCSSFromDOM( clipDOM, clipCSS );
-
- // Get the names of the classes in the target
-
- var targetCSS = new CSSReferenceClassCollection();
-
- // Run the paste buffer converter
-
- var startTime = new Date();
-
- mgr.run( clipDOM, clipCSS, targetDOM, targetCSS, settings );
-
- var endTime = new Date();
- totalTime = ( endTime.getTime() - startTime.getTime() ) / 1000.0;
-
- // Get the debug HTML
-
- var debugData = "";
- if( debug )
- debugData += "<hr><h1>DEBUG</h1>" + mgr.getDebugHTML();
-
- // Insert the paste buffer plus the debug data
-
- var clipHTML = mgr.getClipHTML();
-
- // alert( "clipHTML: " + clipHTML );
-
- targetDOM.insertHTML( prefixHTML + clipHTML + debugData );
- }
- else
- {
- return null;
- }
-
- return totalTime;
- }
-
- function trySmartPaste()
- {
- var result = testPaste( "", "", "", dw.getDocumentDOM(), false, false, null );
- return ( result == null ) ? false : true;
- }
-
- // =============================================================================================================
- // =============================================================================================================
- //
- // Test code
- //
- // =============================================================================================================
- // =============================================================================================================
-